﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Entities;
using CoVanHocTapData.Interface;

namespace CoVanHocTapData.Concretes
{
    public class ChiTietKhoiCTDTRepository:IChiTietKhoiCTDTRepository
    {
        DangKyPhuEntities entities;

        #region repos
        IRangBuocMonRepository rangBuocMonRepos = new RangBuocMonRepository();
        NganhDaoTaoRepository nganhDatTaoRepo = new NganhDaoTaoRepository();
        HeDaoTaoRepository heDaoTaoRepo = new HeDaoTaoRepository();
        NienKhoaRepository nienKhoaRepo = new NienKhoaRepository();
        #endregion

        public ChiTietKhoiCTDTRepository()
        {
            entities = new DangKyPhuEntities();
        }

        #region IChiTietKhoiCTDTRepository Members

        public List<MonHocTheoHocKy> layChiTietKhoiCTDT(string nganhID,int khoiCTDTID,string hocKy)
        {
            
            var ctKhoiCTDT =  (from m in entities.ChiTietKhoiCTDTs
                    join n in entities.MonHocs
                    on m.MonHocID equals n.MonHocID
                    join l in entities.KhoiChuongTrinhDaoTaos
                    on m.KhoiChuongTrinhDaoTaoID equals l.ID
                    where m.KhoiChuongTrinhDaoTaoID == khoiCTDTID
                    select new MonHocTheoHocKy
                    {
                        DVHT = n.SoDVHocTap.Value,
                        HocKy =hocKy,
                        LaMonTuChon = true,
                        MonHocID = n.MonHocID,
                        TenKhoiMonTuChon = l.TenKhoiMonHoc,
                        TenMonHoc = n.TenMonHoc,
                        CungNhom = m.LaCungNhom
                        
                    }
                        );
            string strSongHanh, strTienQuyet, strTruoc;
            List<MonHocTheoHocKy> resultList = new List<MonHocTheoHocKy>();
            foreach (var ctItem in ctKhoiCTDT)
            {
                if (rangBuocMonRepos.LayMonRangBuoc(ctItem.MonHocID, nganhID, out strSongHanh,out strTienQuyet,out strTruoc))
                {
                    ctItem.MonHocSongHanh = strSongHanh;
                    ctItem.MonHocTienQuyet = strTienQuyet;
                    ctItem.MonHocTruoc = strTruoc;
                    resultList.Add(ctItem);
                }
            }
            return resultList;
        }
        #endregion


        public IQueryable<ChiTietKhoiCTDT> LayDSMonTuChonTheoMaSV(string maSV)
        {
            KhoiLopRepository khoiLopRepo = new KhoiLopRepository();
            NganhDaoTao nganhDaoTao = new NganhDaoTao();
            HeDaoTao heDaoTao = new HeDaoTao();
            NienKhoa nienKhoa = new NienKhoa();
            khoiLopRepo.KhoiLop(maSV, out nganhDaoTao, out heDaoTao, out nienKhoa);
            
            var ctKhoiCTDTList = (from m in entities.ChiTietKhoiCTDTs
                                  join n in entities.KhoiChuongTrinhDaoTaos
                                  on m.KhoiChuongTrinhDaoTaoID equals n.ID
                                  where n.NganhID == nganhDaoTao.NganhID
                                  && n.NienKhoaID == nienKhoa.NienKhoaID
                                  && n.HeDaoTaoID == heDaoTao.HeDaoTaoID
                                  select m);

            return ctKhoiCTDTList;

        }




        public IQueryable<ChiTietKhoiCTDT> LayDSMonHocCoCungSoTinChi(int khoiCTDTID, float SOTCMonHoc)
        {
            return from m in entities.ChiTietKhoiCTDTs
                   where m.KhoiChuongTrinhDaoTaoID == khoiCTDTID
                   && m.MonHoc.SoDVHocTap.Value == SOTCMonHoc
                   select m;
        }




        public IQueryable<ChiTietKhoiCTDT> LayDanhSachCungKhoiCTDT(int khoiCTDTID)
        {
            return from m in entities.ChiTietKhoiCTDTs
                   where m.KhoiChuongTrinhDaoTaoID == khoiCTDTID
                   select m;
        }




        #region IChiTietKhoiCTDTRepository Members


        public IQueryable<ChiTietKhoiCTDT> LayDsTheoKhoiCoSoTheoMaMonHoc(string MaMH, string NganhID, int NienKhoaID, string HeDaoTaoID)
        {
            var chiTiet = (from m in entities.ChiTietKhoiCTDTs                          
                          where m.MonHocID.Equals(MaMH)
                          && m.KhoiChuongTrinhDaoTao.LoaiKhoi==2
                          && m.KhoiChuongTrinhDaoTao.HeDaoTaoID.Equals(HeDaoTaoID)
                          && m.KhoiChuongTrinhDaoTao.NganhID.Equals(NganhID)
                          select m).FirstOrDefault();
            if (chiTiet != null)
            {
                return from m in entities.ChiTietKhoiCTDTs
                       where m.KhoiChuongTrinhDaoTaoID == chiTiet.KhoiChuongTrinhDaoTaoID
                       && m.KhoiChuongTrinhDaoTao.HeDaoTaoID.Equals(HeDaoTaoID)
                          && m.KhoiChuongTrinhDaoTao.NganhID.Equals(NganhID)
                       select m;
            }
            return null;
        }

        #endregion
    }
}
